<?php

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\entities\myhtsq\InvoiceBusinessUnitEntity;

/**
 * Description of InvoiceBusinessUnitRepository
 * 开票单位仓储
 * @author chenxc
 */
class InvoiceBusinessUnitRepository extends BaseRepository {

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = []) {
        parent::__construct($config);
    }

    /**
     * 获取开票单位
     * @param string $keyword 搜索关键字
     * @return array 
     */
    public static function getInvoiceBusinessUnit($keyword = '') {
        $query = InvoiceBusinessUnitEntity::find()
                ->select("BUGUID,BUName,BUFullName,BUCode");
        if (!empty($keyword)) {
            $query->where(['like', 'BUName', $keyword]);
        }

        return $query->all();
    }

    /**
     * 获取开票单位
     * @param string $erpGUID
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getInvoiceBusinessUnitByErpGuid($erpGUID)
    {
        return InvoiceBusinessUnitEntity::find()
            ->where(['erp_guid' => $erpGUID])
            ->one();
    }

    /**
     * 根据BUGUID,获取公司名称
     * @param string $companyId
     * @return string $buname
     */
    public static function getBuNameByCompanyId($companyId) {
        return InvoiceBusinessUnitEntity::find()->select('BUName')->where(['BUGUID' => $companyId])->scalar();
    }

    /**
     * 根据BUGUID,获取公司名称
     * @param string $companyIds
     * @return string $buname
     */
    public static function getAllBuNameByCompanyIds($companyIds) {
        return InvoiceBusinessUnitEntity::find()->select('BUName,BUGUID')->where(['BUGUID' => $companyIds])->asArray()->all();
    }

}
